import cv2
import matplotlib.pyplot as plt
import numpy as np


def cv2_show(name, img):
    cv2.imshow(name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


if __name__ == "__main__":
    img_gray = cv2.imread("../images/lena.jpg", cv2.IMREAD_GRAYSCALE)
    print(img_gray)
    # cv2_show("img_gray", img_gray)
    img_rgb = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2RGB)
    print(img_rgb[:,:, 0].shape)
    print(img_rgb[:,:, 1].shape)
    cv2_show("img_rgb", img_rgb)
    cap_test = cv2.VideoCapture("../images/test.mp4")
    cap_fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    # cap_fourcc = cv2.VideoWriter_fourcc(*'XVID')
    cap_fps = cap_test.get(cv2.CAP_PROP_FPS)
    print(cap_fps)
    cap_width = cap_test.get(cv2.CAP_PROP_FRAME_WIDTH)
    cap_height = cap_test.get(cv2.CAP_PROP_FRAME_HEIGHT)
    cap_size = (int(cap_width), int(cap_height))
    cap_writer = cv2.VideoWriter("../images/test_gray.mp4", cap_fourcc, 25, cap_size)
    # cap_writer = cv2.VideoWriter("../images/test_gray.avi", cap_fourcc, 25, cap_size)
    if cap_test.isOpened():
        print("Video open success!")
    else:
        print("Video open failed!")
    while True:
        _, frame = cap_test.read()
        if frame is None:
            break
        frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        frame_rgb = cv2.cvtColor(frame_gray, cv2.COLOR_GRAY2RGB)
        cap_writer.write(frame_rgb)
    cap_test.release()
    cap_writer.release()
